﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SuperSocialShare;
using SuperSocialShare.Clients;

namespace SuperSocialShareTest
{
    public partial class AuthorizePage : PhoneApplicationPage
    {
        private const string SinaAppKey = "";
        private const string SinaAppSecret = "";
        private const string SinaCallbackUrl = "";

        private const string TencentAppKey = "";
        private const string TencentAppSecret = "";
        private const string TencentCallbackUrl = "";

        private const string KaixinAppKey = "";
        private const string KaixinAppSecret = "";
        private const string KaixinCallbackUrl = "";

        private const string RenrenAppKey = "";
        private const string RenrenAppSescret = "";
        private const string RenrenCallbackUrl = "";

        private const string SoHuAppKey = "";
        private const string SoHuAppSecret = "";
        private const string SoHuCallbackUrl = "";

        private const string NetEaseAppKey = "";
        private const string NetEaseAppSecret = "";
        private const string NetEaseCallbackUrl = "";

        private bool _authorizeSucceed = false;
        public AuthorizePage()
        {
            InitializeComponent();
            AuthorizeControl.Client = App.CurrentClient;
            switch (App.SocialType)
            {
                case SocialType.Sina:
                    AuthorizeControl.Client.ClientId = SinaAppKey;
                    AuthorizeControl.Client.ClientSecret = SinaAppSecret;
                    AuthorizeControl.Client.CallbackUrl = SinaCallbackUrl;
                    break;

                case SocialType.Tencent:
                    AuthorizeControl.Client.ClientId = TencentAppKey;
                    AuthorizeControl.Client.ClientSecret = TencentAppSecret;
                    AuthorizeControl.Client.CallbackUrl = TencentCallbackUrl;
                    break;

                case SocialType.Renren:
                    AuthorizeControl.Client.ClientId = RenrenAppKey;
                    AuthorizeControl.Client.ClientSecret = RenrenAppSescret;
                    AuthorizeControl.Client.CallbackUrl = RenrenCallbackUrl;
                    break;

                case SocialType.KaiXin:
                    AuthorizeControl.Client.ClientId = KaixinAppKey;
                    AuthorizeControl.Client.ClientSecret = KaixinAppSecret;
                    AuthorizeControl.Client.CallbackUrl = KaixinCallbackUrl;
                    break;

                case SocialType.SoHu:
                    AuthorizeControl.Client.ClientId = SoHuAppKey;
                    AuthorizeControl.Client.ClientSecret = SoHuAppSecret;
                    AuthorizeControl.Client.CallbackUrl = SoHuCallbackUrl;
                    break;
                case SocialType.NetEase:
                    AuthorizeControl.Client.ClientId = NetEaseAppKey;
                    AuthorizeControl.Client.ClientSecret = NetEaseAppSecret;
                    AuthorizeControl.Client.CallbackUrl = NetEaseCallbackUrl;
                    break;
                default:
                    break;
            }
            AuthorizeControl.AuthorizeCompleted += AuthorizeControl_AuthorizeCompleted;
            AuthorizeControl.BeginRequest();
        }

        void AuthorizeControl_AuthorizeCompleted(AsyncEventArgs e)
        {
            if (e.Error == null)
            {
                _authorizeSucceed = true;
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Authorize Succeed!");
                    NavigationService.Navigate(new Uri("/SharePage.xaml", UriKind.Relative));
                });
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            if (_authorizeSucceed)
            {
                if (NavigationService.CanGoBack)
                    NavigationService.RemoveBackEntry();
            }
        }
    }
}